package edu.zjut.hjg.saas.biz.service.impl;

import edu.zjut.hjg.saas.biz.dao.BulletinDao;
import edu.zjut.hjg.saas.biz.dao.UserLogDao;
import edu.zjut.hjg.saas.biz.model.Bulletin;
import edu.zjut.hjg.saas.biz.model.UserLog;
import edu.zjut.hjg.saas.biz.service.BulletinService;
import edu.zjut.hjg.saas.biz.support.OperateType;

public class BulletinServiceImpl implements BulletinService {

    private BulletinDao bulletinDao;
    private UserLogDao  userLogDao;

    public void setBulletinDao(BulletinDao bulletinDao) {
        this.bulletinDao = bulletinDao;
    }

    @Override
    public Bulletin findByAppId(int appId) {
        return bulletinDao.findByAppId(appId);
    }

    public void setUserLogDao(UserLogDao userLogDao) {
        this.userLogDao = userLogDao;
    }

    @Override
    public void edit(int appId, String content, int userId, String operator) {
        Bulletin bulletin = bulletinDao.findByAppId(appId);
        boolean update = true;
        if (bulletin == null) {
            bulletin = new Bulletin();
            update = false;
        }
        bulletin.setContent(content);
        bulletin.getKindergarten().setId(appId);
        bulletin.getUser().setId(userId);

        UserLog userLog = new UserLog();
        userLog.setKindergarten(bulletin.getKindergarten());
        userLog.setLoginId(operator);
        if (update) {
            bulletinDao.update(bulletin);
            userLog.setOperateType(OperateType.UPDATE);
            userLog.setDescription("修改了公告");
        } else {
            bulletinDao.create(bulletin);
            userLog.setOperateType(OperateType.INSERT);
            userLog.setDescription("添加了公告");
        }
        userLogDao.create(userLog);
    }
}
